iT邦幫忙

2025 iThome 鐵人賽

DAY 30
0
自我挑戰組

cpe30天練習系列 第 30

cpe練習day30

  • 分享至 

  • xImage
  •  

今天是練習cpe的Satellites題目

程式碼

#include <bits/stdc++.h>
using namespace std;
 
int main() 
{
   	double s ,a;
   	double r = 6440;
   	string unit;
	double rad ,chord ,arc;  
   	while(cin >> s >> a >> unit)
   	{
   		if(unit == "min")
		{
			a /= 60.0; 
		}
		if(a > 180)
		{
			a=360 - a;
		}
		rad = M_PI / 180 * a;
		chord = 2 * (r+s) * sin(rad/2);
		arc = rad * (r+s); 
		cout << fixed << setprecision(6) << arc << " " << chord << " " <<endl;
	}
    return 0;
}

解題方向

if(unit == "min")
{
	a /= 60.0; 
}
  • 若輸入單位是minutes(角分),要換成 degrees(角度)
if(a > 180)
{
	a=360 - a;
}
  • 因為題目規定要取「較短 的那條弧」,若角度 > 180°,那就取對面的那一半(即 360°−a)
  • rad = M_PI / 180 * a; -> 將角度轉成弧度
  • chord = 2 * (r + s) * sin(rad / 2); -> 計算弦長 (chord)
  • arc = rad * (r + s); -> 計算弧長 (arc)

上一篇
cpe練習day29
系列文
cpe30天練習30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言